import {computed} from 'vue'
import {useData} from 'vitepress'
import {useLang} from './lang'
import {defaultLang} from '../constant'
import {createGiteeUrl} from '../utils'

export function useEditLink() {
    const {page, theme, frontmatter} = useData()
    const lang = useLang()
    const editLink = {}

    const canEditSource = computed(() => {
        return lang.value === defaultLang
    })

    const url = computed(() => {
        if (canEditSource.value) {
            const {
                repo,
                docsDir = '',
                docsBranch = 'dev',
                docsRepo = repo,
                editLinks,
            } = theme.value
            const showEditLink =
                frontmatter.value.editLink != null
                    ? frontmatter.value.editLink
                    : editLinks
            const {relativePath} = page.value
            if (!showEditLink || !relativePath || !repo) {
                return null
            }
            return createGiteeUrl(
                docsRepo,
                docsDir,
                docsBranch,
                relativePath,
                '',
                ''
            )
        }
    })
    const text = computed(() => {
        return canEditSource.value
            ? editLink['edit-on-gitee'] : ''
    })

    return {
        url,
        text,
    }
}
